package com.ceair.mub2c.payment.payments.controller;

import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ceair.flight.logging.enums.Source;
import com.ceair.mub2c.payment.payments.service.CeairWalletService;
import com.ceair.mub2c.payment.payments.utils.WalletUtils;
import com.ceair.mub2c.payment.payments.vo.AccountVerifyVO;
import com.ceair.mub2c.payment.payments.vo.WalletAcquireReq;
import com.ceair.mub2c.payment.payments.vo.WalletAcquireRes;
import com.ceair.mub2c.payment.payments.vo.WalleterDto;
import com.ceair.mub2c.payment.payments.vo.WalleterPwdVO;

/**
 * @author fengwei16
 * @Description: TODO
 * @date: 2018年12月29日 上午9:09:11
 */

@Source(name = "40000146")
@Controller
@RequestMapping(value = "/wallet")
public class WalletController {
    Logger log = Logger.getLogger(getClass());
    @Autowired
    private CeairWalletService ceairWalletService;
	
	/**
	 * @param SCCode
	 * @param SubSCCode
	 * @param accountId
	 * @param walletPwd
	 * @return 获取钱包用户详情
	 */
	@Source(name = "40001426")
	@RequestMapping(value = "/userQuery", method = RequestMethod.GET)
	@ResponseBody
	public WalleterDto userQuery(@RequestParam(required = true) String SCCode, @RequestParam(required = true) String SubSCCode,
			@RequestParam(required = true) String accountId, @RequestParam(required = true) String walletPwd) {
		WalleterDto walleter = new WalleterDto();
		if ("".equals(accountId) || accountId == null) {
			walleter.setRespDesc("常客未登录");
			return walleter;
		}
		String reqId = UUID.randomUUID().toString().toLowerCase();
		WalletAcquireReq walleterReq = new WalletAcquireReq(SCCode, SubSCCode, reqId, null, accountId, "mufly", "ceair", "1");
		walleter = ceairWalletService.getUserInfo(walleterReq);
		return walleter;
	}
	
	/**
	 * @param SCCode
	 * @param SubSCCode
	 * @param accountId
	 * @param walletPwd
	 * @return 验证钱包用户密码
	 */
	@Source(name = "40001426")
	@RequestMapping(value = "/accountVerifyPwd", method = RequestMethod.GET)
	@ResponseBody
	public WalleterPwdVO accountVerifyPwd(@RequestParam(required = true) String SCCode, @RequestParam(required = true) String SubSCCode,
			@RequestParam(required = true) String accountId, @RequestParam(required = true) String walletPwd) {
		WalleterPwdVO walleterPwd = new WalleterPwdVO();
		if ("".equals(walletPwd) || walletPwd == null) {
			walleterPwd.setRespDesc("请输入钱包密码");
			return walleterPwd;
		}
		String reqId1 = UUID.randomUUID().toString().toLowerCase();
		String reqId2 = UUID.randomUUID().toString().toLowerCase();
		AccountVerifyVO accountVerify = new AccountVerifyVO();
		accountVerify.setScCode(SCCode);
		accountVerify.setSubScCode(SubSCCode);
		accountVerify.setCustomerId(accountId);
		accountVerify.setReqId(reqId2);
		accountVerify.setUserType("mufly");
		accountVerify.setOrgCode("ceair");
		//获取随机数
		WalletAcquireReq walletRandomReq = new WalletAcquireReq(SCCode, SubSCCode, reqId1, null, accountId, "mufly", "ceair", "1");
		WalletAcquireRes walletRandomAcquireRes = ceairWalletService.getWalletRandomAcquire(walletRandomReq);
		accountVerify.setRandJnlNo(walletRandomAcquireRes.getRandJnlNo());
		accountVerify.setRandom(walletRandomAcquireRes.getRandom());
		//加密,正式公钥未配置
		String walletPwdRSA = null;
		try {
			walletPwdRSA = WalletUtils.encryptRSA(walletPwd,walletRandomAcquireRes.getRandom());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("RSA加密异常");
			walleterPwd.setRespDesc("RSA加密异常");
			return walleterPwd;
		}
		accountVerify.setPwdResult(walletPwdRSA);
		WalleterPwdVO accountVerifyPwd = ceairWalletService.accountVerifyPwd(accountVerify);
		return accountVerifyPwd;
	}

}

















